//
//  ViewController.m
//  05_ScrollView练习
//
//  Created by 徐昊 on 16/7/20.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#define FRAME self.view.frame
#define WIDTH FRAME.size.width
#define HEIGHT FRAME.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;

@property (nonatomic, strong) UIScrollView *album;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeAlbum];
    [self addPage];
    [self addTimer];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)makeAlbum {
    self.album = [[UIScrollView alloc] initWithFrame:FRAME];
    [self.view addSubview:self.album];
    NSArray *arr = [NSArray arrayWithObjects:@"guidePage_04.png", @"guidePage_01.png", @"guidePage_02.png", @"guidePage_03.png", @"guidePage_04.png", @"guidePage_01.png", nil];
    self.album.contentSize = CGSizeMake(WIDTH * arr.count, HEIGHT);
    self.album.pagingEnabled = YES;
    self.album.bounces = NO;
    self.album.delegate = self;
    for (NSInteger i = 0; i < arr.count ; i++) {
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, HEIGHT)];
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        smallScrollView.delegate = self;
        img.image = [UIImage imageNamed:arr[i]];
        [smallScrollView addSubview:img];
        [self.album addSubview:smallScrollView];
        smallScrollView.tag = i;
        smallScrollView.minimumZoomScale = 0.5;
        smallScrollView.maximumZoomScale = 2;
        smallScrollView.bounces = NO;
    }
    self.album.contentOffset = CGPointMake(WIDTH, 0);
    self.album.bounces = NO;
    self.album.showsHorizontalScrollIndicator = NO;
}
- (void)addPage{
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
    self.page.currentPage = 0;
    self.page.numberOfPages = 4;
    [self.view addSubview:self.page];
    [self.page addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
}
- (void)addTimer {
    self.timer =  [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)nextImage {
    NSInteger page = self.page.currentPage;
    if (page == 3) {
        page = 0;
    } else {
        page++;
    }
    self.album.contentOffset = CGPointMake((page + 1) * WIDTH, 0);
    self.page.currentPage = page;
    for (UIScrollView *scroll in self.album.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1;
        }
    }
}
- (void)closeTimer {
    [self.timer invalidate];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.page.currentPage = index - 1;
    if (index == 5) {
        self.page.currentPage = 0;
        scrollView.contentOffset = CGPointMake(WIDTH * 1, 0);
    }
    if (index == 0) {
        self.page.currentPage = 4;
        scrollView.contentOffset = CGPointMake(WIDTH * 4, 0);
    }
    for (UIScrollView *scroll in scrollView.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1;
        }
    }
//    UIScrollView *scroll = [scrollView.subviews objectAtIndex:_index];
//    scroll.zoomScale = 1;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self closeTimer];
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    [self addTimer];
}
- (void)click:(UIPageControl *)page {
    self.album.contentOffset = CGPointMake(WIDTH * (page.currentPage + 1), 0);
    for (UIScrollView *scroll in self.album.subviews) {
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1;
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
